<%@page import="net.sf.json.JSONObject"%>
<%@page import="java.io.PrintWriter"%>
<%@page import="java.io.File"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@page import="java.text.DecimalFormat"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	request.setCharacterEncoding("UTF-8");
	//获取文件的上传的具体目录，就是你的web项目的真实路径
	String realPath = request.getSession().getServletContext().getRealPath("/");
	//定义上传的目录
	String dirPath = realPath+"/upload";
	File dirFile = new File(dirPath);
	//自动创建上传的目录
	if(!dirFile.exists())dirFile.mkdirs();
	//上传操作  
	FileItemFactory factory = new DiskFileItemFactory();  
		ServletFileUpload upload = new ServletFileUpload(factory);  
	String fileName = null;
	HashMap<String,Object> map = new HashMap<String,Object>();
		try{  
	     List items = upload.parseRequest(request);
	  if(null != items){  
	         Iterator itr = items.iterator();  
	         while(itr.hasNext()){  
	             FileItem item = (FileItem)itr.next();  
	             if(item.isFormField()){  
	                 continue;  
	             }else{  
	                fileName = UUID.randomUUID().toString()+getExt(item.getName());
				 //上传文件的目录
	                File savedFile = new File(dirPath,fileName);  
	                item.write(savedFile);  
	                map.put("name",item.getName());//文件的重视名称
	                map.put("size",item.getSize());//文件的真实大小
	                map.put("sizeString",countFileSize(item.getSize()));//获取文件转换以后的大写
	                map.put("url","upload/"+fileName);//获取文件的具体服务器的目录
	             }  
	         }  
	  }  
	}catch(Exception e){  
	     e.printStackTrace();  
	} 
 	
		
	response.setContentType("text/html; charset=UTF-8");
	PrintWriter pw = response.getWriter();
	pw.println("<script> parent.addAppendix('"+map.get("name")+"','"+map.get("url")+"'); history.back();</script>");
	pw.flush();
	pw.close();
%>

<%!
/**
 * 文件大小转换成格式化的字符串
 */
public static String countFileSize(long fileSize) {
	String fileSizeString = "";
	try {
		DecimalFormat df = new DecimalFormat("#.00");
		long fileS = fileSize;
		if (fileS == 0) {
			fileSizeString = "0KB";
		} else if (fileS < 1024) {
			fileSizeString = df.format((double) fileS) + "B";
		} else if (fileS < 1048576) {
			fileSizeString = df.format((double) fileS / 1024) + "KB";
		} else if (fileS < 1073741824) {
			fileSizeString = df
					.format(((double) fileS / 1024 / 1024) - 0.01)
					+ "MB";
		} else {
			fileSizeString = df.format((double) fileS / 1024 / 1024 / 1024)
					+ "G";
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	return fileSizeString;
}


/**
 * 获取一个文件的后缀(带有点)
 * 
 * @param fileName
 *            文件名
 * @return 返回文件的后缀
 */
public static String getExt(String fileName) {
	int pos = fileName.lastIndexOf(".");
	if (pos == -1)
		return "";
	return fileName.substring(pos, fileName.length());
}

/**
 * 获取一个文件的后缀(不带有点)
 * 
 * @param fileName
 *            文件名
 * @return 返回文件的后缀
 */
public static String getExtNoPoint(String fileName) {
	if (fileName.lastIndexOf(".") == -1)
		return "";
	int pos = fileName.lastIndexOf(".") + 1;
	return fileName.substring(pos, fileName.length());
}
%>